import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/internal/api/comic_api.dart';
import 'package:easy_read_comic/models/page_model.dart';
import 'package:easy_read_comic/widgets/auto_image_widget.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:seo_renderer/seo_renderer.dart' as SR;
import '../../models/comic_model.dart';
import '../../models/navigate_model.dart';

class IndexListView extends StatefulWidget {
  final NavigateModel navigate;

  IndexListView(this.navigate, {key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _IndexListView();
}

class _IndexListView extends State<IndexListView> {
  List<ComicModel> _list = [];

  int _current_page = 0;

  bool _load_done = false;

  @override
  void initState() {
    super.initState();
    this.getList();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      child: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 10.0,
          crossAxisSpacing: 10.0,
          childAspectRatio: 0.55,
        ),
        itemBuilder: (BuildContext context, int index) {
          return SR.LinkRenderer(
            text: _list[index].Title,
            href: "/detail/${_list[index].Id}",
            child: GestureDetector(
              child: Container(
                child: Column(
                  children: [
                    Container(
                      height: 150,
                      child: ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                        child: AutoImage.buildImage(
                          _list[index].Thumb,
                          alt: _list[index].Title,
                        ),
                      ),
                    ),
                    Container(
                      alignment: Alignment.centerLeft,
                      child: SR.TextRenderer(
                        child: Text(
                          _list[index].Title,
                          style: TextStyle(
                            fontSize: 16,
                          ),
                          maxLines: 1,
                        ),
                      ),
                    ),
                    Container(
                      alignment: Alignment.centerLeft,
                      child: SR.TextRenderer(
                        child: Text(
                          _list[index].Tags.join(" ").toString(),
                          style: TextStyle(
                            color: Colors.black26,
                          ),
                        ),
                      ),
                      padding: EdgeInsets.only(
                        right: 5,
                      ),
                    ),
                  ],
                ),
              ),
              onTap: () {
                Get.toNamed("/detail/${_list[index].Id}");
              },
            ),
          );
        },
        itemCount: _list.length,
      ),
      onLoad: () async {
        getList();
      },
      onRefresh: () async {
        resetGetList();
      },
    );
  }

  void getList() {
    if (_load_done) {
      BotToast.showText(text: "已全部加载完成");
      return;
    }
    ComicApi()
        .list(navigateId: widget.navigate.Id, currentPage: _current_page)
        .then((response) {
      for (var item in response.data["data"]["List"] ?? []) {
        this._list.add(ComicModel.formJson(item));
      }
      var page = PageModel.fromJson(response.data["data"]["Page"]);
      if (!page.hasNextPage) {
        _load_done = true;
      } else {
        _current_page = page.NextPage;
      }
      setState(() {});
    });
  }

  void resetGetList() {
    _current_page = 0;
    _list = [];
    _load_done = false;
    this.getList();
  }
}
